package util

import (
	"errors"
	"gin_cli/pkg/logging"
	"reflect"
	"strings"

	"github.com/astaxie/beego/validation"
)

//  参数验证
func Validate(valid validation.Validation) (map[string]string, error) {
	data := make(map[string]string)
	if valid.HasErrors() {
		for _, err := range valid.Errors {
			logging.Info(err.Message)
			data[err.Key] = err.Message
		}

		return data, errors.New("请求参数验证出错")
	}
	return data, nil
}
func StructToMap(obj interface{}) map[string]interface{} {
	t := reflect.TypeOf(obj)
	v := reflect.ValueOf(obj)
	var data = make(map[string]interface{})
	for i := 0; i < t.NumField(); i++ {
		data[t.Field(i).Name] = v.Field(i).Interface()
	}
	return data
}
func GetStructTagJson(
	stucPointer interface{},
	stuc interface{},
) map[string]interface{} {

	t := reflect.TypeOf(stucPointer).Elem()
	v := reflect.ValueOf(stuc)
	m := make(map[string]interface{})
	for i := 0; i < t.NumField(); i++ {
		str := t.Field(i).Tag.Get("json")
		strArr := strings.Split(str, ",")
		m[strArr[0]] = v.Field(i).Interface()
	}
	return m
}

// func Float64ToInt64(num float64) {
// 	val := int64(num)
// }
